package com.tecode.controller.song;

import com.tecode.bean.Admin;
import com.tecode.bean.Music;
import com.tecode.bean.Musicrejection;
import com.tecode.service.song.SongService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping(value = "/song")
public class SongController {
    @Autowired
    private SongService songService;

    @RequestMapping(value = "/getList")
    public String getList(Map<String, Object> map) {
        map.put("musics", songService.getList());
        map.put("count", songService.countMusics());
        return "product-list.html";
    }

    @PostMapping(value = "/audit")
    @ResponseBody
    public Map audit(@RequestParam(value = "musicId") int musicId,
                     @RequestParam(value = "cid") int cid,
                     HttpSession session) {
        Admin admin = (Admin)session.getAttribute("admin");
        int auditorid=admin.getAdminid();
        Map<String, Object> map = new HashMap<String, Object>();
        String msg = "";
        Integer audit = songService.audit(musicId, cid,auditorid);
        if (audit >= 1) {
            msg = "审核成功";
        } else {
            msg = "审核失败";
        }
        map.put("msg", msg);
        map.put("musicId",musicId);
        return map;
    }

    @RequestMapping(value = "/getAuditList/{cid}")
    public String getAuditList(@PathVariable(value = "cid") int cid, Map<String, Object> map) {
        String view = "";
        map.put("musics", songService.getAuditList(cid));
        map.put("count", songService.countAudit(cid));
        view = getString(cid, view);
        return view;
    }

    @RequestMapping(value = "/reject")
    @ResponseBody
    public Map reject(@RequestParam(value = "musicId") int musicId,
                      @RequestParam(value = "cid") int cid,
                      @RequestParam(value = "reason") String reason,
                      HttpSession session) {
        Admin admin = (Admin)session.getAttribute("admin");
        int userid=admin.getAdminid();
        Map<String, Object> map = new HashMap<String, Object>();
        Musicrejection musicrejection = new Musicrejection();
        musicrejection.setMusicid(musicId);
        musicrejection.setUserid(userid);
        String msg = "";
        Boolean aBoolean=false;
        if (StringUtils.isNotEmpty(reason.trim())){
            musicrejection.setReason(reason);
            aBoolean = songService.reject(musicId, cid, musicrejection,userid);
        }else {
            msg="请输入不少于10个字";
            map.put("msg", msg);
            return map;
        }
        if (aBoolean) {
            msg = "提交成功";
        } else {
            msg = "提交不成功";
        }
        map.put("msg", msg);
        return map;
    }

    @RequestMapping(value = "/reason/{musicId}")
    public String reason(@PathVariable(value = "musicId") int musicId, HashMap<String, Object> map) {
        map.put("musicId", musicId);
        return "musicRejectReason-.html";
    }

    @RequestMapping(value = "/search")
    public String search(@RequestParam(value = "searchName", required = false) String searchName,
                         @RequestParam(value = "searchNickName", required = false) String searchNickName,
                         @RequestParam(value = "cid", required = false) Integer cid,
                         Map<String, Object> map) {
        String msg = "";
        if ("".equals(searchName)&&"".equals(searchNickName)){
            msg="请至少填一个搜索项";
        }else {
            map.put("musics",songService.search(searchName,searchNickName,cid));
            map.put("count",songService.countSearch(searchName,searchNickName,cid));
        }
        String view = "";
        if (cid==null||"".equals(cid)){
            view = "product-list.html";
        }else {
            view = getString(cid, view);
        }

        map.put("msg", msg);
        return view;

    }
    @RequestMapping(value = "/shiting/{musicId}")
    @ResponseBody
    public Music shiting(@PathVariable(value = "musicId") Integer musicId){
        return songService.selectByPrimaryKey(musicId);
    }

    @RequestMapping(value = "/getInfo")
    public String getInfo(@RequestParam(value = "musicId")int musicId,
                          @RequestParam(value = "cid")int cid,
                          Map<String, Object> map){
        map.put("info",songService.getInfo(musicId,cid));
        if (cid==12){
            return "music-reject-show.html";
        }else if(cid==10){
            return "music-show.html";
        }else {
            return "music-audit-show.html";
        }

    }

    private String getString( Integer cid, String view) {
        switch (cid) {
            case 11:
                view = "song-audit.html";
                break;
            case 10:
                view = "song-audited.html";
                break;
            case 12:
                view = "song-rejection.html";
        }
        return view;
    }

}
